package cn.example.zjgsu.service.Impl;

import cn.example.zjgsu.dao.*;
import cn.example.zjgsu.exception.MyException;
import cn.example.zjgsu.pojo.*;
import cn.example.zjgsu.pojo.result.ResultFood;
import cn.example.zjgsu.pojo.result.ResultMFR;
import cn.example.zjgsu.pojo.result.ResultShop;
import cn.example.zjgsu.service.HomePageServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author xuejiahui
 * @date 2020/3/15 -20:16
 */

@Service
public class HomePageServerImpl implements HomePageServer{

    @Autowired
    FoodMapper foodMapper;

    @Autowired
    CityMapper cityMapper;

    @Autowired
    ShopMapper shopMapper;

    @Autowired
    FoodRecordMapper foodRecordMapper;

    @Autowired
    CommentMapper commentMapper;



    @Override
    public List<ResultFood> getNationalSelection() {
        List<Food> foods= foodMapper.getTheBestFood();
        System.out.println(foods.size());
        List<ResultFood> resultFoods = new ArrayList<ResultFood>();

        for (Food food:foods){
            ResultFood resultFood =new ResultFood();
            resultFood.setId(food.getId());
            resultFood.setIntroduction(food.getIntroduction());
            resultFood.setName(food.getName());
            resultFood.setCityid(food.getCityid());
            String pictures=food.getPictures();
            String a []= pictures.split(";");
            ArrayList  b = new ArrayList();
            for(int i=0;i<a.length;i++){
                b.add(a[i]);
            }
            resultFood.setPictures(b);
            resultFoods.add(resultFood);
        }
        return resultFoods;
    }

    @Override
    public int getCityIdByName(String cityname) {
        return cityMapper.getCityId(cityname);
    }

    @Override
    public City getCityInformation(int cityid) {
        return cityMapper.selectByPrimaryKey(cityid);
    }

    @Override
    public List<ResultFood> getFoodByCityId(int cityid) {
        List<Food> foods =foodMapper.getCityFood(cityid);
        List<ResultFood> resultFoods = new ArrayList<ResultFood>();

        for (Food food:foods){
            ResultFood resultFood =new ResultFood();
            resultFood.setId(food.getId());
            resultFood.setIntroduction(food.getIntroduction());
            resultFood.setName(food.getName());
            String pictures=food.getPictures();
            String a []= pictures.split(";");
            ArrayList  b = new ArrayList();
            for(int i=0;i<a.length;i++){
                b.add(a[i]);
            }
            resultFood.setPictures(b);
            resultFood.setCityid(food.getCityid());
            resultFoods.add(resultFood);
        }
        return resultFoods;
    }

    @Override
    public List<ResultShop> getShopByCityId(int cityid) {
        List<Shop> shops= shopMapper.selectCityShop(cityid);
        List<ResultShop> resultShops =new ArrayList<>();
        for(Shop shop :shops){
            ResultShop resultShop=new ResultShop();
            resultShop.setAddress(shop.getAddress());
            resultShop.setId(shop.getId());
            resultShop.setName(shop.getName());
            resultShop.setPrice(shop.getPrice());
            resultShop.setScore(shop.getScore());
            resultShop.setIntroduction(shop.getIntroduction());
            resultShop.setLabel(shop.getLabel());
            String pictures = shop.getPictures();
            String a[] =pictures.split(";");
            ArrayList b= new ArrayList();
            for(int i =0;i<a.length;i++){
                b.add(a[i]);
            }
            resultShop.setPictures(b);
            resultShops.add(resultShop);
        }
        return resultShops;
    }

    @Override
    public List<ResultFood> getFoodByShopId(int shopid) {
        Shop shop =shopMapper.getFoodByShopId(1);
        List<FoodAndShop>  foodAndShops1 =shop.getFoodAndShops();
        List<ResultFood> resultFoods = new ArrayList<ResultFood>();
        for (FoodAndShop foodAndShop: foodAndShops1) {
            ResultFood resultFood=new ResultFood();
            resultFood.setId(foodAndShop.getFood().getId());
            resultFood.setIntroduction(foodAndShop.getFood().getIntroduction());
            resultFood.setName(foodAndShop.getFood().getName());
            resultFood.setPictures(Arrays.asList(foodAndShop.getFood().getPictures().split(";")));
            resultFoods.add(resultFood);
        }
        return resultFoods;
    }

    @Override
    public List<ResultShop> getShopByFoodId(int foodid) {
        Food food =foodMapper.getShopByFoodid(1);
        List<FoodAndShop>  foodAndShops =food.getFoodAndShops();
        List<ResultShop> resultShops =new ArrayList<>();
        for (FoodAndShop foodAndShop: foodAndShops) {
            ResultShop resultShop=new ResultShop();
            resultShop.setAddress(foodAndShop.getShop().getAddress());
            resultShop.setId(foodAndShop.getShop().getId());
            resultShop.setName(foodAndShop.getShop().getName());
            resultShop.setPrice(foodAndShop.getShop().getPrice());
            resultShop.setScore(foodAndShop.getShop().getScore());
            resultShop.setLabel(foodAndShop.getShop().getLabel());
            resultShop.setIntroduction(foodAndShop.getShop().getIntroduction());
            resultShop.setPictures(Arrays.asList(foodAndShop.getShop().getPictures().split(";")));
            resultShops.add(resultShop);
        }
        return resultShops;
    }

    @Override
    public List<ResultMFR> getFoodRecordByCityID(int cityid) {
        List<ResultMFR> cityFoodRecords = foodRecordMapper.getTheCityFoodRecord(cityid);
        for (ResultMFR cityFoodRecord : cityFoodRecords) {
            cityFoodRecord.setCommentnumber(commentMapper.getCommentNumberOfFD(cityFoodRecord.getId()));
            String[] Pictures = cityFoodRecord.getPictures().split(";");
            cityFoodRecord.setPictures(Pictures[0]);
        }
        return cityFoodRecords;
    }

    @Override
    public ResultShop getShopByShopId(int shopid) {
        Shop shop = shopMapper.selectByPrimaryKey(shopid);
        ResultShop resultShop = new ResultShop();
        resultShop.setAddress(shop.getAddress());
        resultShop.setId(shop.getId());
        resultShop.setName(shop.getName());
        resultShop.setPrice(shop.getPrice());
        resultShop.setScore(shop.getScore());
        resultShop.setIntroduction(shop.getIntroduction());
        resultShop.setLabel(shop.getLabel());
        resultShop.setPictures(Arrays.asList(shop.getPictures().split(";")));
        return resultShop;
    }

    @Override
    public List<ResultFood> fuzzyQueryFood(String foodname) throws MyException{
        List<Food> foods = foodMapper.listFood(foodname);
        List<ResultFood> resultFoods = new ArrayList<ResultFood>();
        if (!foods.isEmpty()){
            for (Food food:foods){
                ResultFood resultFood =new ResultFood();
                resultFood.setId(food.getId());
                resultFood.setIntroduction(food.getIntroduction());
                resultFood.setName(food.getName());
                resultFood.setPictures(Arrays.asList(food.getPictures().split(";")));
                resultFood.setCityid(food.getCityid());
                resultFoods.add(resultFood);
            }
            return resultFoods;
        }else{
            throw new MyException("没有对应的食物");
        }
    }
}
